<template>
  <div class="container">
    <div class="btnList">
      <el-date-picker
        v-model="erpCenter.date"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
      ></el-date-picker>

      <el-button
        size="small"
        type="success"
        class="handleSearch"
        icon="el-icon-check"
        @click="pull()"
        >拉取ERP</el-button
      >
    </div>
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.code"
        placeholder="退货单编码"
        clearable
      ></el-input>

      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索机构信息"
        v-model="organizationList.name"
      >
        <template slot="prepend">机构</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="organizationBtn"
        ></el-button>
      </el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="制单开始日期"
        end-placeholder="制单结束日期"
      ></el-date-picker>
      <el-select v-model="search.refund_type" placeholder="请选择类型">
        <el-option
          v-for="item in typeList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.refund_status" placeholder="请选择状态">
        <el-option
          v-for="item in statusList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.dictdetailsid" placeholder="请选择来源">
        <el-option
          v-for="item in dictdetailsidList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="d_date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="审核开始日期"
        end-placeholder="审核结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="org_name"
        label="店铺名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="org_address"
        label="店铺地址"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="serial_code"
        label="退货单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="refund_code"
        label="商慧7单号"
        align="center"
      ></el-table-column>
      <!-- <el-table-column
        prop="sheet_no"
        label=" 回退单号"
        align="center"
      ></el-table-column> -->
      <el-table-column
        prop="created_at"
        label="退货申请时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="examine_time"
        label="审核时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="审核状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dictdetailsid"
        label="来源"
        align="center"
      ></el-table-column>
     
      <el-table-column label="操作" width="380" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="handleEdit(scope)"
            >详情</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="confirms(scope)"
            >审核</el-button
          >
          <!-- <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="noConfirms(scope)"
            >不入库审核</el-button
          > -->
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="primary"
            icon="el-icon-edit-outline"
            @click="edit(scope)"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            type="info"
            size="mini"
            icon="el-icon-back"
            @click="rollback(scope)"
            >回退</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- 详情Dailog -->
    <superSalesReturnDialog :dialog="dialog"></superSalesReturnDialog>
    <!-- 编辑 -->
    <editSalesReturnDialog :editDialog="editDialog"></editSalesReturnDialog>
    <organizationDialog
      :organizatioDialog="organizatioDialog"
      @tag="supplierTag"
    ></organizationDialog>
  </div>
</template>
<script>
import { parseTime, getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import SuperSalesReturnDialog from "@/components/salesReturn/superSalesReturnDialog";
import EditSalesReturnDialog from "@/components/salesReturn/editSalesReturnDialog";
import OrganizationDialog from "@/components/superGrossMargin/organizationDialog";
import {
  selectRefundOrderList,
  pullRefundData,
  checkRefundOrder,
  backReturnOrder,
  checkRefundOrderUnStock,
  getSourceData,
} from "@/api/salesReturn/salesReturnList";
import { getToken } from "@/utils/auth";

export default {
  components: {
    pagination: Pagination,
    superSalesReturnDialog: SuperSalesReturnDialog,
    editSalesReturnDialog: EditSalesReturnDialog,
    organizationDialog: OrganizationDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      search: {
        code: "",

        start_date: "",
        end_date: "",
        d_start_date: "",
        d_end_date: "",
        refund_type: "",
        refund_status: "",
        dictdetailsid: "",
      },
      organizatioDialog: {
        centerDialogVisible: false,
      },
      organizationList: {
        name: "",
        code: "",
      },
      //dialog详情信息
      dialog: {
        centerDialogVisible: false,
        id: "",
      },
      editDialog: {
        centerDialogVisible: false,
        id: "",
      },
      date: [],
      d_date: [],
      erpCenter: {
        date: "",
        refund_type: "",
      },
      typeList: [
        {
          value: 1,
          label: "中仓退货单",
        },
        {
          value: 2,
          label: "商慧7退货单",
        },
      ],
      dictdetailsidList: [
        // {
        //   value: 290,
        //   label: "粮油",
        // },
        // {
        //   value: 287,
        //   label: "超市",
        // },
        // {
        //   value: "0_287",
        //   label: "折扣超市",
        // },
        // {
        //   value: 295,
        //   label: "面食",
        // },
        // {
        //   value: 384,
        //   label: "中仓休食",
        // },
        // {
        //   value: 385,
        //   label: "中仓干果",
        // },
        // {
        //   value: 386,
        //   label: "中仓炒货",
        // },
      ],
      statusList: [
        {
          value: 0,
          label: "全部",
        },
        {
          value: 1,
          label: "待审核",
        },
        {
          value: 2,
          label: "已审核",
        },
        {
          value: 3,
          label: "已回退",
        },
      ],
    };
  },
  created() {
    //页面数据
    this.getData();
    this.getSourceDataList();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        code: this.search.code,
        org_code: this.organizationList.code,
        refund_type: this.search.refund_type,
        status: !this.search.refund_status ? 0 : this.search.refund_status,
        start_date: this.search.start_date,
        end_date: this.search.end_date,
        d_start_date: this.search.d_start_date,
        d_end_date: this.search.d_end_date,
        dictdetailsid: this.search.dictdetailsid,
      };
      selectRefundOrderList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.paginate.data.forEach((item) => {
              if (item.status == 1) {
                item.statusMsg = "待审核";
              } else if (item.status == 2) {
                item.statusMsg = "已审核";
              } else if (item.status == 3) {
                item.statusMsg = "已回退";
              }
            });
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //获取来源
    getSourceDataList() {
      let data = {
        module:2
      }
      getSourceData(data).then((res) => {
        if (res.code == 0) {
          this.dictdetailsidList = res.data.source;
        }
      });
    },
    //超级机构搜索
    organizationBtn() {
      this.organizatioDialog = {
        centerDialogVisible: true,
      };
    },
    // 机构搜索
    supplierTag(data) {
      this.organizationList = {
        name: "",
        code: "",
      };
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.organizationList.name = arrName.join(",");
      this.organizationList.code = arrCode.join(",");
      console.log(this.organizationList, 11111);
    },
    //回退
    rollback(scope) {
      this.$confirm("是否回退?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            idstr: scope.row.id,
          };

          backReturnOrder(data).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: res.msg || res.message,
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //编辑
    edit(scope) {
      this.editDialog.centerDialogVisible = true;
      this.editDialog.id = scope.row.id;
    },
    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_date = this.date[0];
        this.search.end_date = this.date[1];
      }
      if (this.d_date.length > 0) {
        this.search.d_start_date = this.d_date[0];
        this.search.d_end_date = this.d_date[1];
      }
      this.pageData.total = null;
      this.pageData.page = 1;
      this.getData();
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/refund/exportRefundOrderGoods?token=" +
            getToken() +
            "&code=" +
            this.search.code +
            "&org_code=" +
            this.organizationList.code +
            "&refund_type=" +
            this.search.refund_type +
            "&status=" +
            this.search.refund_status +
            "&dictdetailsid=" +
            this.search.dictdetailsid +
            "&start_date=" +
            this.search.start_date +
            "&end_date=" +
            this.search.end_date +
            "&d_start_date=" +
            this.search.d_start_date +
            "&d_end_date=" +
            this.search.d_end_date;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //拉取erp
    pull(scope) {
      let data = {
        pull_date: this.erpCenter.date,
      };
      this.$confirm("是否拉取ERP?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          pullRefundData(data)
            .then((res) => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消拉取ERP",
          });
        });
    },
    //审核
    confirms(scope) {
      this.$confirm("是否审核?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            idstr: scope.row.id,
          };
          checkRefundOrder(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg || res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //不入库审核
    // noConfirms(scope) {
    //   this.$confirm("是否不入库审核?", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(() => {
    //       let data = {
    //         idstr: scope.row.id,
    //       };
    //       checkRefundOrderUnStock(data).then((res) => {
    //         if (res.code == 0) {
    //           this.getData();
    //           this.$message({
    //             type: "success",
    //             message: res.msg || res.message,
    //           });
    //         }
    //       });
    //     })
    //     .catch(() => {
    //       this.$message({
    //         type: "info",
    //         message: "已取消",
    //       });
    //     });
    // },
    //详情
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.id = scope.row.id;
    },
    //清空
    empty() {
      this.date = [];
      this.d_date = [];
      this.search = {
        code: "",
        start_date: "",
        end_date: "",
        d_start_date: "",
        d_end_date: "",
        refund_type: "",
        refund_status: "",
        dictdetailsid: "",
      };
      this.organizationList = {
        name: "",
        code: "",
      };
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>